# -*- coding: utf-8 -*-
#
# Tubemaster
#
# Copyright (C) 2009 Selim Ok <selimok@gmail.com>
# Copyright (C) 2007-2008 Siegfried-Angel Gevatter Pujals <siggi.gevatter@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#

import threading, subprocess,constants
from os import devnull

class GetOriginalTitle(threading.Thread):
	"""Open a pipe to `youtube-dl' and ask for the original title of the video."""
	
	def __init__(self, videoId, script):
		
		self.videoId = videoId
		self.script = script
		self.result = None
		
		threading.Thread.__init__(self)
	
	def run(self):	
		try:
			# Call youtube-dl and ask for the download url
			self.pipe = subprocess.Popen(
					str(self.script) + ' --get-title '+ constants.youtube_prefix + str(self.videoId), 
					shell=True, 
					stdin=None,
					stdout=subprocess.PIPE,
					stderr=open(devnull, 'w'),
				)
			
			# Read the resulting output
                        try:
                            self.result = self.pipe.stdout.readline()
                            self.result = str(self.result)[:-1]
		        except:
                            #TODO:put debug info
                            pass
		except IOError:
                        #TODO:put debug info
			pass	# The process was killed, probably because the user closed the GUI
